package cn.sxt.yxl.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.users.dao.IAAdminsDao;
import cn.sxt.yxl.users.dao.IARoleDao;
import cn.sxt.yxl.users.pojo.AAdmins;
import cn.sxt.yxl.users.pojo.ARole;
import cn.sxt.yxl.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	/**
	 * 查询单个管理员
	 */
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {
		return adminsDao.findOne(condMap);
	}
	/**
	 * 添加单个管理员
	 */
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject jsonObject = new JSONObject();
		int res=adminsDao.saveOne(admins);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", admins.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
		
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject jsonObject = new JSONObject();
		int res=adminsDao.updateOne(admins);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", admins.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
		
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins) {
		JSONObject jsonObject = new JSONObject();
		int res=adminsDao.deleteOne(admins);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", admins.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
		
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil!=null){
			//分页
			Page<?> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			//查询一遍
			List<AAdmins> adminsList = this.adminsDao.findCondList(condMap);
			//设置总记录数
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return adminsList;
			
		}
		//不分页
		return adminsDao.findCondList(condMap);
	}
	
	/**
	 * 查询单个角色
	 */
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap) {
		return roleDao.findOne(condMap);
	}
	/**
	 * 添加单个角色
	 */
	@Override
	public JSONObject saveOneRoleService(ARole role) {
		JSONObject jsonObject = new JSONObject();
		int res=roleDao.saveOne(role);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", role.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
			
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	@Override
	public JSONObject updateOneRoleService(ARole role) {
		JSONObject jsonObject = new JSONObject();
		int res=roleDao.updateOne(role);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", role.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
			
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	
	@Override
	public JSONObject deleteOneRoleService(ARole role) {
		JSONObject jsonObject = new JSONObject();
		int res=roleDao.deleteOne(role);
		if(res>0){
			JSONObject dateObject = new JSONObject();
			dateObject.put("id", role.getId()+"");
			dateObject.put("effectCount", res);
			
			jsonObject.put("date", dateObject);
			jsonObject.put("code", 0);
			jsonObject.put("info", "成功");
		}else{
			
			jsonObject.put("code", 1);
			jsonObject.put("info", "失败");
		}
		return jsonObject;
	}
	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil!=null){
			//分页
			Page<?> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			//查询一遍
			List<ARole> roleList = this.roleDao.findCondList(condMap);
			//设置总记录数
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return roleList;
			
		}
		//不分页
		return roleDao.findCondList(condMap);
	}
	

	
}
